package com.googlecode.textish;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

import com.googlecode.textish.parser.TextishParser;
import com.googlecode.textish.tokenizer.DocumentToken;
import com.googlecode.textish.tokenizer.TextishLexicalAnalyzer;

/**
 * Class that provides methods for formatting Textish. The list of formatters is
 * empty by default; consider using a specific subclass for your needs.
 * <strong>This class is not threadsafe.</strong>
 * 
 * @author cgdavies
 * 
 */
public class TextishEngine {

	private TextishLexicalAnalyzer m_lexer;

	private TextishParser m_parser;

	private List m_formatters;

	public TextishEngine() {
		m_lexer = new TextishLexicalAnalyzer();
		m_parser = new TextishParser();
		m_formatters = new ArrayList();
	}

	/**
	 * Takes input text and formats it according to the configured formatters.
	 * 
	 * @param text the input text
	 * @return the processed, output text
	 */
	public CharSequence format(CharSequence text) {
		DocumentToken document = m_lexer.analyze(text);
		StringWriter output = new StringWriter();

		try {
			m_parser.parse(text, document, output);
			return output.toString();
		}
		catch (IOException ex) {
			throw new TextishRuntimeException(ex);
		}
	}

	/**
	 * @return the list of formatters to apply during formatting
	 */
	public List getFormatters() {
		return m_formatters;
	}

	/**
	 * @param formatters the list of formatters to apply during formatting
	 */
	public void setFormatters(List formatters) {
		m_formatters = formatters;
	}

}
